package com.iakuil.app.api.controller;


import com.iakuil.app.common.core.BaseController;
import com.iakuil.app.common.core.annotation.Log;
import com.iakuil.app.common.core.domain.Result;
import com.iakuil.app.common.core.enums.OpType;
import com.iakuil.app.common.core.jsr303.constraints.Ids;
import com.iakuil.app.domain.dto.AuthorDto;
import com.iakuil.app.domain.dto.cmd.AuthorAdd;
import com.iakuil.app.domain.dto.cmd.AuthorEdit;
import com.iakuil.app.domain.dto.query.AuthorQuery;
import com.iakuil.app.domain.dto.vo.AuthorInfoVo;
import com.iakuil.app.domain.entity.Author;
import com.iakuil.app.service.AuthorService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 作者 接口
 *
 * @author Kai
 * @date 2023-12-05
 */
@Tag(name = "作者（示例）", description = "作者接口")
@RestController
@RequestMapping("/api/v1/author")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AuthorController extends BaseController {
    private final AuthorService authorService;

    @Operation(summary = "作者详情", description = "根据ID查询作者。")
    @Parameter(name = "id", description = "ID", required = true, in = ParameterIn.QUERY)
    @GetMapping
    public Result<AuthorInfoVo> doQuery(@RequestParam Long id) {
        Author entity = authorService.getById(id);
        return success(entity, AuthorInfoVo.class);
    }

    @Operation(summary = "作者列表", description = "分页查询作者数据。")
    @PostMapping(value = "/list")
    public Result<AuthorDto> doPaging(@RequestBody AuthorQuery param) {
        Author entity = convert(param, Author.class);
        startPage();
        return pageInfo(authorService.list(entity, param.getFields())).map(e -> convert(e, AuthorDto.class));
    }

    @Operation(summary = "作者新增", description = "新增作者。")
    @Log(title = "作者", type = OpType.INSERT)
    @PostMapping(value = "/create")
    public Result<Long> doCreate(@Validated @RequestBody AuthorAdd param) {
        Author entity = convert(param, Author.class);
        return success(authorService::save, entity);
    }

    @Operation(summary = "作者修改", description = "修改作者。")
    @Log(title = "作者", type = OpType.UPDATE)
    @PostMapping(value = "/edit")
    public Result<Void> doEdit(@Validated @RequestBody AuthorEdit param) {
        Author entity = convert(param, Author.class);
        return success(authorService.modify(entity));
    }

    @Operation(summary = "作者删除", description = "批量删除作者。")
    @Parameter(name = "ids", description = "ID列表，逗号分隔，无需方括号", required = true, in = ParameterIn.QUERY, example = "10001,10002")
    @Log(title = "作者", type = OpType.DELETE)
    @PostMapping(value = "/remove")
    public Result<Void> doRemove(@RequestParam @Ids Long[] ids) {
        authorService.removeByIds(ids);
        return success();
    }
}
